const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    login_type: 1, //登录方式
    copy_right: {
      is_load: -1
    },
    username: '', //用户名
    password: '', //密码
    code: '', //验证码图
    verify_code: '', //验证码
    mobile: '', //手机号
    out_code: '', //动态验证码
    second: 0, //计时
    code_config: {
      value: {}
    }, //验证码配置
    login_config: {}, //登录配置
    animationData: {},
    loginFlag: 0,
    getOutCodeFlag: 0,
    is_first_bind: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;

    app.copyRightLoad(that);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    app.restStatus(that, 'loginFlag');
    app.restStatus(that, 'sendCodeFlag');
    app.restStatus(that, 'findPasswordFlag');

    app.sendRequest({
      url: 'System.Login.loginConfig',
      data: {},
      success: function(res) {
        let code = res.code;
        let data = res.data;
        console.log(res);
        if (code == 0) {
          let code_config = data.code_config;
          let login_config = data.login_config;

          // 验证码获取
          if (code_config.value.pc == 1) {
            app.verificationCode(that);
          }

          that.setData({
            code_config: code_config,
            login_config: login_config
          })
        }
      }
    });
    // 加载消息配置
    app.noticeConfig(that);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 输入账号
   */
  inputUsername: function(e) {
    let that = this;
    let username = e.detail.value;

    that.setData({
      username: username
    })
  },

  /**
   * 输入密码
   */
  inputPassword: function(e) {
    let that = this;
    let password = e.detail.value;

    that.setData({
      password: password
    })
  },

  /**
   * 输入验证码
   */
  inputVerify: function(e) {
    let that = this;
    let verify_code = e.detail.value;

    that.setData({
      verify_code: verify_code
    })
  },

  /**
   * 输入手机号
   */
  inputMobile: function(e) {
    let that = this;
    let mobile = e.detail.value;

    that.setData({
      mobile: mobile
    })
  },

  /**
   * 输入动态验证码
   */
  inputOutCode: function(e) {
    let that = this;
    let out_code = e.detail.value;

    that.setData({
      out_code: out_code
    })
  },

  /**
   * 选择登录方式
   */
  loginType: function(e) {
    let that = this;
    let login_type = e.currentTarget.dataset.type;

    that.setData({
      login_type: login_type
    })
  },

  /**
   * 切换验证码
   */
  switchVerificationCode: function(e) {
    let that = this;
    app.verificationCode(that);
  },

  /**
   * 忘记密码-弹框
   */
  forgetPassword: function(e) {
    let that = this;
    let animation = wx.createAnimation({
      duration: 777,
      transformOrigin: "50% 50%",
    })

    that.animation = animation;

    animation.opacity(1).step();

    that.setData({
      animationData: animation.export(),
      forget_show: 1
    })
  },

  /**
   * 关闭弹框
   */
  closeMask: function(e) {
    let that = this;
    let animation = wx.createAnimation({
      timingFunction: 'step-start',
      transformOrigin: "50% 50%",
    })

    that.animation = animation;

    animation.opacity(0).step();

    that.setData({
      animationData: animation.export(),
      forget_show: 0
    })
  },

  /**
   * 获取动态码
   */
  outCode: function(e) {
    let that = this;

    let mobile = that.data.mobile;
    let verify_code = that.data.verify_code;
    let code_config = that.data.code_config;
    let getOutCodeFlag = that.data.getOutCodeFlag;

    if (getOutCodeFlag == 1) {
      return false;
    }
    app.clicked(that, 'getOutCodeFlag');

    if (mobile == '') {
      app.showBox(that, '手机号不可为空');
      app.restStatus(that, 'getOutCodeFlag');
      return false;
    }

    if (!app.getRegex(mobile, 'mobile')) {
      app.showBox(that, '请输入正确的手机号');
      app.restStatus(that, 'getOutCodeFlag');
      return false;
    }

    if (code_config.value != undefined && code_config.value.pc == 1) {
      if (verify_code == '') {
        app.showBox(that, '验证码不可为空');
        app.restStatus(that, 'getOutCodeFlag');
        return false;
      }
      app.checkHasMobile(that, mobile, 'checkVerificationCode', false, 'getOutCodeFlag', 'getOutCode', verify_code);
    } else {
      app.checkHasMobile(that, mobile, 'getOutCodeFlag', false, 'getOutCodeFlag');
    }
  },

  /**
   * 获取动态验证码
   */
  getOutCode: function() {
    let that = this;
    let mobile = that.data.mobile;

    app.sendRequest({
      url: "System.Login.sendRegisterMobileCode",
      data: {
        mobile: mobile,
        key: app.globalData.openid,
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;

        if (code == 0) {
          if (data.code == 0) {
            let second = 120;
            let timer = setInterval(function() {
              second--;
              that.setData({
                second: second
              })
              if (second == 0) {
                clearInterval(timer);
              }
            }, 1000)
          }
          app.showBox(that, data.message);

          if (data.code < 0) {
            app.verificationCode(that);
          }

          app.restStatus(that, 'getOutCodeFlag');
        } else {
          app.restStatus(that, 'getOutCodeFlag');
        }
      }
    });
  },

  /**
   * 账号登录
   */
  login: function(e) {
    let that = this;
    let username = that.data.username;
    let password = that.data.password;
    let verify_code = that.data.verify_code;
    let mobile = that.data.mobile;
    let out_code = that.data.out_code;
    let code_config = that.data.code_config;
    let notice_config = that.data.notice_config;
    let login_type = that.data.login_type;
    let loginFlag = that.data.loginFlag;

    if (loginFlag == 1) {
      return false;
    }
    app.clicked(that, 'loginFlag');

    if (login_type == 1) {
      if (username == '') {
        app.showBox(that, '用户名不可为空');
        app.restStatus(that, 'loginFlag');
        return false;
      }

      if (password == '') {
        app.showBox(that, '密码不可为空');
        app.restStatus(that, 'loginFlag');
        return false;
      }
    }

    if (login_type == 2) {
      if (mobile == '') {
        app.showBox(that, '手机号不可为空');
        app.restStatus(that, 'loginFlag');
        return false;
      }

      if (!app.getRegex(mobile, 'mobile')) {
        app.showBox(that, '请输入正确的手机号');
        app.restStatus(that, 'loginFlag');
        return false;
      }

      if (notice_config.noticeMobile == 1) {
        if (out_code == '') {
          app.showBox(that, '动态码不可为空');
          app.restStatus(that, 'loginFlag');
          return false;
        }
      }
    }

    if (code_config.value != undefined) {
      if (code_config.value.pc == 1) {
        if (verify_code == '') {
          app.showBox(that, '验证码不可为空');
          app.restStatus(that, 'loginFlag');
          return false;
        }
      }
    }

    if (code_config.value != undefined && code_config.value.pc == 1) {
      let commit_type = notice_config.noticeMobile == 1 && login_type == 2 ? 'checkOutCode' : 'commit';
      app.checkVerificationCode(that, verify_code, commit_type, 'loginFlag'); // 检测图文验证码

    } else if (notice_config.noticeMobile == 1 && login_type == 2) {
      this.checkOutCode(); // 检测动态验证码

    } else {
      this.commit(); // 直接提交
    }
  },

  /**
   * 检测动态验证码
   */
  checkOutCode: function() {
    let mobile = this.data.mobile;
    let out_code = this.data.out_code;

    app.checkOutCode(this, out_code, 'mobile', mobile, 'loginFlag');
  },

  /**
   * 登录提交
   */
  commit: function(e) {
    let that = this;
    let username = that.data.username;
    let password = that.data.password;
    let mobile = that.data.mobile;
    let login_type = that.data.login_type;
    let is_first_bind = app.globalData.is_first_bind;
    let sourceid = app.globalData.sourceid;

    let bind_message_info = '';
    if (is_first_bind == 1) {
      bind_message_info = app.globalData.bind_message_info;
    }
    let url = login_type == 1 ? 'System.Login.login' : 'System.Login.mobileLogin';
    let param = {
      is_first_bind: is_first_bind,
      bind_message_info: bind_message_info,
      sourceid: sourceid
    }
    if (login_type == 1) {
      param.username= username;
      param.password= password;
    } else {
      param.mobile = mobile;
      param.sms_captcha = sms_captcha;
    }

    app.sendRequest({
      url: url,
      data: param,
      success: function(res) {
        let code = res.code;
        let data = res.data;
        let message = res.message;
        console.log(res);
        if (code == 0) {
          app.restStatus(that, 'loginFlag');
          app.globalData.is_login = 1;
          app.globalData.is_logout = 0;
          app.globalData.token = data.token;
          app.globalData.is_first_bind = 0;
          app.globalData.bind_message_info = '';
          app.setSourceId('');
          if (!app.isNotLogin(1)) {
            wx.switchTab({
              url: '/pages/member/member/member',
            })
          }
        } else {
          app.verificationCode(that);
          app.showBox(that, message);
          app.restStatus(that, 'loginFlag');
        }
      }
    })
  }
})